package org.example.controller;

import com.alibaba.fastjson.JSONObject;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import org.example.Main;
import org.example.config.FileName;
import org.example.model.Plan;
import org.example.model.PlanEntity;
import org.example.service.FileHandleService;
import org.example.service.PlanService;
import org.example.utils.IdWorker;
import org.example.view.PlanAddDialogView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

/**
 * @Author: wwy
 * @Date: 2020/11/26 13:27
 */
public class PlanAddDialogController implements Initializable {
    private static final Logger LOGGER = LoggerFactory.getLogger(PlanAddDialogController.class);
    public static ApplicationContext applicationContext;
    public static boolean flag = false;
    @FXML
    private TextField planNameField;
    @FXML
    private TextField priorityField;
    private static Stage dialogStage;

    private static Plan currentPlan;
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        LOGGER.info("planadd初始化完成");
        if(currentPlan != null){
            planNameField.setText(currentPlan.getPlanName());
            priorityField.setText(currentPlan.getPriority());
        }


    }


    /**
     * 打开新增弹条件出框
     */
    public boolean showPlanAddDialog() {
        currentPlan = null;
        // Load the fxml file and create a new stage for the popup dialog.
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("view/PlanAddDialogView.fxml"));
        try {
            AnchorPane page = (AnchorPane) loader.load();
            // Create the dialog Stage.
            dialogStage = new Stage();
            dialogStage.setTitle("Add plan");
            dialogStage.initModality(Modality.WINDOW_MODAL);
            dialogStage.initOwner(Main.getStage());
            Scene scene = new Scene(page);
            dialogStage.setScene(scene);
            dialogStage.showAndWait();
            return flag;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return flag;
    }

    public boolean showPlanEditDialog(Plan plan) {
        // Load the fxml file and create a new stage for the popup dialog.
        currentPlan = plan;
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("view/PlanAddDialogView.fxml"));
        try {
            AnchorPane page = (AnchorPane) loader.load();
            // Create the dialog Stage.
            dialogStage = new Stage();
            dialogStage.setTitle("edit plan");
            dialogStage.initModality(Modality.WINDOW_MODAL);
            dialogStage.initOwner(Main.getStage());
            Scene scene = new Scene(page);
            dialogStage.setScene(scene);
            dialogStage.showAndWait();
            return flag;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return flag;
    }
    @FXML
    public void handleCancel(){
        dialogStage.close();
        flag = false;
    }
    @FXML
    public void handleOk(){
        if(currentPlan == null){
            //新增
            dialogStage.close();
            flag = true;
            //进行对象封装
            long nextId = IdWorker.getNextId();
            Plan plan = new Plan(nextId,planNameField.getText(),priorityField.getText());
            PlanEntity planEntity = new PlanEntity();
            planEntity.setId(nextId);
            planEntity.setPlanName(planNameField.getText());
            planEntity.setPriority(priorityField.getText());
            System.out.println(plan.toString());
            PlanService planService = applicationContext.getBean(PlanService.class);
            planService.save(planEntity);
            JavaProcessManagementController javaProcessManagementController = applicationContext.getBean(JavaProcessManagementController.class);
            javaProcessManagementController.setPlan(plan);
        }else{
            dialogStage.close();
            flag = true;
            //修改
            //获取修改对象的id
            long id = currentPlan.getId();
            PlanEntity planEntity = new PlanEntity();
            planEntity.setId(id);
            planEntity.setPlanName(planNameField.getText());
            planEntity.setPriority(priorityField.getText());
            PlanService planService = applicationContext.getBean(PlanService.class);
            planService.save(planEntity);
            JavaProcessManagementController javaProcessManagementController = applicationContext.getBean(JavaProcessManagementController.class);
            Plan plan1 = new Plan(id,planNameField.getText(),planNameField.getText());
            javaProcessManagementController.setEditPlan(plan1);
        }

    }
}
